<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include2923 :: header('工程概算台账新增')"/>
</head>
<body>
<div id="uploadPopup">
    <div class="layui-form-item">
        <label class="layui-form-label">批次号</label>
        <div class="layui-input-block">
            <input type="text" id="inputField" placeholder="请输入批次信息" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="uploadButton">上传文件</button>
        </div>
    </div>
</div>
<th:block th:include="include2923 :: footer"/>

<script>
    layui.use(['layer', 'form'], function(){
        var layer = layui.layer;
        var form = layui.form;

        var MODULE_PATH = rootPath + "/InfoProjectbudgetCsController/";

        // 监听上传按钮点击事件
        layui.$('#uploadButton').on('click', function(){
            var inputValue = layui.$('#inputField').val();
            if(inputValue.trim() === ''){
                layer.msg('批次号不能为空，不能打开文件上传功能');
            } else {
                // 显示文件上传框
                var inputFile = document.createElement('input');
                inputFile.type = 'file';
                inputFile.multiple = true;
                inputFile.accept = '*'; // 可根据需要设置接受的文件类型
                inputFile.onchange = function(e){
                    var files = e.target.files;
                    if(files.length > 0){
                        // 调用文件上传函数
                        uploadFiles(files,inputValue);
                    } else {
                        layer.msg('请选择要上传的文件');
                    }
                };

                inputFile.click();
            }
        });

        // 文件上传函数
        function uploadFiles(files,batchnum){

            var formData = new FormData();
            for(var i = 0; i < files.length; i++){
                if (!(files[i].name.endsWith('.xlsx')||files[i].name.endsWith('.xls'))) {
                    layer.msg('文件中存在文件格式不合规，请上传.xls或.xlsx格式文件', {icon: 2, time: 3000});
                    return;
                }
                formData.append('files', files[i]);
            }
            formData.append('type',3);
            formData.append('batchnum',batchnum);
            showLoad()
            // 发送Ajax请求
            $.ajax({
                url: MODULE_PATH + 'uploadJgBatch', // 后端上传接口
                type: 'POST',
                data: formData,
                contentType: false,
                processData: false,
                success: function(res) {
                    if (res.code == 0) {
                        closeLoad(showLoad());//回调成功后关闭动画
                        layer.msg(res.msg, {icon: 1, time: 3000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("infoProjectbudgetCs-table");
                        });
                    } else {
                        closeLoad(showLoad());//回调成功后关闭动画
                        layer.msg(res.msg, {icon: 2, time: 3000});
                    }
                },
                error: function() {
                    layer.msg('上传失败');
                }
            });
        }

        function showLoad() {
            return layer.msg('数据导入中，请耐心等待...', {
                icon: 6,
                shade: [0.5, '#f5f5f5'],
                scrollbar: false,
                offset: 'auto',
                time: 1000000
            });
        }

        function closeLoad(index) {
            layer.close(index);
        }
    });
</script>
</body>
</html>